package cn.yiufung.qxquartz.common;

import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.idgen.YitIdHelper;

/**
 * @author 姚锋
 * @createTime 2023年08月23日 14:37:00
 */
public class ApiResponse<T> {


    private static IdGeneratorOptions options = new IdGeneratorOptions((short) 42);

    static {
        YitIdHelper.setIdGenerator(options);
    }


    /**
     * 状态码
     */
    private StatusCode status;

    /**
     * 信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 返回时间戳
     */
    private long timestamp;

    /**
     * 接口路径
     */
    private String path;

    /**
     * 请求ID
     */
    private String requestId;

    public StatusCode getStatus() {
        return status;
    }

    public void setStatus(StatusCode status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    /**
     * 成功响应
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setStatus(StatusCode.SUCCESS);
        response.setMessage(StatusCode.SUCCESS.getMessage());
        response.setData(data);
        response.setTimestamp(System.currentTimeMillis());
        response.setRequestId(generateRequestId());
        // 设置其他默认值
        return response;
    }

    /**
     * 成功响应
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> success() {
        ApiResponse<T> response = new ApiResponse<>();
        response.setStatus(StatusCode.SUCCESS);
        response.setMessage(StatusCode.SUCCESS.getMessage());
        response.setTimestamp(System.currentTimeMillis());
        response.setRequestId(generateRequestId());
        // 设置其他默认值
        return response;
    }

    /**
     * 失败响应
     * @param status
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> error(StatusCode status, String message) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setStatus(status);
        response.setMessage(message);
        response.setTimestamp(System.currentTimeMillis());
        response.setRequestId(generateRequestId());
        // 设置其他默认值
        return response;
    }

    /**
     * 未找到资源
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> notFound() {
        ApiResponse<T> response = new ApiResponse<>();
        response.setStatus(StatusCode.NOT_FOUND);
        response.setMessage(StatusCode.NOT_FOUND.getMessage());
        response.setTimestamp(System.currentTimeMillis());
        response.setRequestId(generateRequestId());
        // 设置其他默认值
        return response;
    }

    /**
     * 服务器内部错误
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> internalServerError(String message) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setStatus(StatusCode.INTERNAL_SERVER_ERROR);
        response.setMessage(message);
        response.setTimestamp(System.currentTimeMillis());
        response.setRequestId(generateRequestId());
        // 设置其他默认值
        return response;
    }

    /**
     * 生成请求ID
     * @return
     */
    private static String generateRequestId() {
        return String.valueOf(YitIdHelper.nextId());
    }
}
